package cn.edu.scujcc.myprojecttest;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import cn.edu.scujcc.InterFace.LibraryApi;
import cn.edu.scujcc.myproject.R;
import cn.edu.scujcc.stType.LibraryUserInfo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LibraryUserInfoActivity extends AppCompatActivity {
    public static final String TAG = "MAIN";
    public String username;//定义全局变量用户名和密码
    public String password;

    //实例化UI层的textview
    private TextView studentNameTextView;
    private TextView studentIdTextView;
    private TextView facultyTextView;
    private TextView borrowedTextView;
    private TextView availableTextView;
    private TextView overtimeTextView;
    private TextView booksDataTextView;

    //在主线程创建handler用于接收，子线程message压栈的待处理消息队列
    private  Handler h = new Handler() {
        //当收到消息时，应该执行的代码
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                Toast.makeText(LibraryUserInfoActivity.this,"访问出错",
                        Toast.LENGTH_LONG).show();
            }else if (msg.what == 1) {
                LibraryUserInfo info = (LibraryUserInfo) msg.obj;
//                List<LibraryUserInfo> libraryUserInfo = (List<LibraryUserInfo>) msg.obj;
//                LibraryUserInfo Userinfo = new LibraryUserInfo();
//                Userinfo.setStudentName(info.getStudentName());
                Log.d("TEST", info.toString());

                //改变textview文本
                studentNameTextView.setText(info.getStudentName());
                studentIdTextView.setText(info.getStudentId());
                facultyTextView.setText(info.getFaculty());
                borrowedTextView.setText(info.getBorrowed());
                availableTextView.setText(info.getAvailable());
                overtimeTextView.setText(info.getOvertime());
//                booksDataTextView.setText(info.getBooksData());//借阅书籍详情打不出来

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library_userinfo);
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);//清除

        studentNameTextView = findViewById(R.id.studentName);
        studentIdTextView = findViewById(R.id.studentId);
        facultyTextView = findViewById(R.id.faculty);
        borrowedTextView = findViewById(R.id.borrowed);
        availableTextView = findViewById(R.id.available);
        overtimeTextView = findViewById(R.id.overtime);
        booksDataTextView = findViewById(R.id.booksData);


        //接收LibraryLogin传过来的参数参数
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        password = intent.getStringExtra("password");
        Toast.makeText(LibraryUserInfoActivity.this,"填写学号："+username+"\n填写密码："+password,
                Toast.LENGTH_LONG).show();

        testLibraryUserInfo();
    }


    private void testLibraryUserInfo(){
        //实例化一个请求对象 api
        LibraryApi api = NetworkFactory.LibraryApi();
//        Call<LibraryUserInfo> l = api.getLibraryUserInfo(username,password);
// 传参调用api网络请求
        Call<LibraryUserInfo> l=api.getLibraryUserInfo(username,password);
        l.enqueue(new Callback<LibraryUserInfo>() {

            @Override
            public void onResponse(Call<LibraryUserInfo> call, Response<LibraryUserInfo> response) {
                LibraryUserInfo info = response.body();//实例化一个userinfo对象，将网络请求响应body内容给对象
                Log.d(TAG, "名字：" + info.getStudentName());
                Log.d(TAG, "学号：" + info.getStudentId());
                Log.d(TAG, "院系：" + info.getFaculty());
                Log.d(TAG, "已借本数：" + info.getBorrowed());
                Log.d(TAG, "可借本数：" + info.getAvailable());
                Log.d(TAG, "超时本数：" + info.getOvertime());
//                Toast.makeText(MainActivity.this,"名字："+info.getStudentName()+"\n学号："+ info.getStudentId()+
//                                "\n院系："+info.getFaculty()+"\n已借本数："+info.getBorrowed()+"\n可借本数："+info.getAvailable()+"\n超时本数："+info.getOvertime(),
//                        Toast.LENGTH_LONG).show();

                Message msg = new Message();
                msg.what = 1;
                msg.obj = info;
                h.sendMessage(msg);//用handler将json反序列化对象堆栈到MessageQueue


            }

            @Override
            public void onFailure(Call<LibraryUserInfo> call, Throwable t) {
                //http请求失败todo

            }
        });
    }
}